<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="110px">
      <el-form-item label="兑换码名称" prop="typeName">
        <el-input
          v-model="queryParams.typeName"
          placeholder="请输入兑换码名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="金额" prop="money">
        <el-input
          v-model="queryParams.money"
          placeholder="请输入金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="兑换码状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择兑换码状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>

      </el-form-item>-->
      <el-form-item label="兑换码值" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入兑换码值"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:exchange:export']"
        >导出
        </el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="exchangeList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="序号" align="center">
        <template slot-scope="scope">
          {{queryParams.pageSize * (queryParams.pageNum - 1) + scope.$index + 1 }}
        </template>
      </el-table-column>
      <!--      <el-table-column label="活动id" align="center" prop="companyId" />-->
      <!--      <el-table-column label="立减金类型" align="center" prop="type" />-->
      <el-table-column label="兑换码名称" align="center" prop="typeName"/>
      <el-table-column label="金额 " align="center" prop="money"/>
<!--      <el-table-column label="兑换码状态" align="center" prop="status" :formatter="statusFormat"/>-->
      <el-table-column label="兑换码值" align="center" prop="code"/>
      <el-table-column label="数量" align="center" prop="num"/>
     <!-- <el-table-column label="兑换人" align="center" prop="openId"/>
      <el-table-column label="兑换时间" align="center" prop="exchangeTime"/>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-edit"-->
          <!--            @click="handleUpdate(scope.row)"-->
          <!--            v-hasPermi="['system:exchange:edit']"-->
          <!--          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            v-if="scope.row.status==0&&scope.row.deleted==1"
            @click="handleStatusChange(scope.row)"
            v-hasPermi="['system:exchange:remove']"
          >启用
          </el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.status==0&&scope.row.deleted==0"
            @click="handleStatusChange(scope.row)"
            v-hasPermi="['system:exchange:remove']"
          >停用
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改兑换码记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="立减金类型">
          <el-select v-model="form.type" placeholder="请选择立减金类型">
            <el-option label="请选择字典生成" value=""/>
          </el-select>
        </el-form-item>
        <el-form-item label="业务类型名称" prop="typeName">
          <el-input v-model="form.typeName" placeholder="请输入业务类型名称"/>
        </el-form-item>
        <el-form-item label="金额 " prop="money">
          <el-input v-model="form.money" placeholder="请输入金额 "/>
        </el-form-item>
        <el-form-item label="兑换码状态(数据字典0-未兑换 1-已兑换)">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="0-未删 1-已删" prop="deleted">
          <el-input v-model="form.deleted" placeholder="请输入0-未删 1-已删"/>
        </el-form-item>
        <el-form-item label="兑换码值" prop="code">
          <el-input v-model="form.code" placeholder="请输入兑换码值"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listExchange,
    getExchange,
    delExchange,
    addExchange,
    updateExchange,
    exportExchange
  } from "@/api/system/exchange";

  export default {
    name: "Exchange",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 兑换码记录表格数据
        exchangeList: [],
        // 状态数据字典
        statusOptions: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          companyId: undefined,
          type: undefined,
          typeName: undefined,
          money: undefined,
          status: undefined,
          deleted: undefined,
          code: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.queryParams.companyId = this.$route.params && this.$route.params.companyId;
      this.getList();
      this.getDicts("sys_exchange_status").then(response => {
        this.statusOptions = response.data;
      });
    },
    methods: {
      /** 查询兑换码记录列表 */
      getList() {
        this.loading = true;
        listExchange(this.queryParams).then(response => {
          this.exchangeList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 兑换码状态修改
      handleStatusChange(row) {
        let text = row.deleted == "0" ? "停用" : "启用";
        this.$confirm('是否' + text + '该兑换码?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          const data = {
            exchangeId: row.exchangeId,
            deleted: row.deleted == "0" ? 1 : 0
          }
          return updateExchange(data);
        }).then(() => {
          this.msgSuccess(text + "成功");
          this.handleQuery();
        }).catch(function () {
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          exchangeId: undefined,
          companyId: undefined,
          type: undefined,
          typeName: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined,
          money: undefined,
          status: "0",
          deleted: undefined,
          remark: undefined,
          code: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 字典状态字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.status);
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.exchangeId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加兑换码记录";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const exchangeId = row.exchangeId || this.ids
        getExchange(exchangeId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改兑换码记录";
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.exchangeId != undefined) {
              updateExchange(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addExchange(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const exchangeIds = row.exchangeId || this.ids;
        this.$confirm('是否确认删除兑换码记录编号为"' + exchangeIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delExchange(exchangeIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有兑换码记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportExchange(queryParams);
        }).then(response => {
          window.location.href = process.env.VUE_APP_BASE_API + "/common/download?fileName=" + encodeURI(response.msg) + "&delete=" + false;
        }).catch(function () {
        });
      }
    }
  };
</script>
